import { _decorator, Component, Button, Label, Sprite, Node, input, Input } from 'cc';
import { Databus } from '../core/Databus';
import { ResourceManager } from '../framework/ResourceManager';
import { UserInfo } from '../platform/UserInfo';
import { WxSdkManager } from '../platform/ad/WxSdkManager';

const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('UserCenterPage')
export class UserCenterPage extends Component {
  private userInfoImage: Node = null;
  private backButton: Button = null;
  private userLevelLogoSprite: Node = null;
  private userLevelLogoText: Node = null;
  private playLevelText: Label = null;
  private goldCoinText: Label = null;
  private winNumText: Label = null;
  private loseNumText: Label = null;
  private winRateText: Label = null;
  private hasLoadLevelImage = false;
  onLoad(): void {
    this.backButton = this.node.getChildByName('BackButton').getComponent(Button);
    this.backButton.node.on(Button.EventType.CLICK, this.backPage, this);

    this.node.getChildByName('LevelIntroSprite').active = false;
    this.playLevelText = this.node.getChildByName('UserInfoRow1').getChildByName('UserPlayLevelValue').getComponent(Label);
    this.goldCoinText  = this.node.getChildByName('UserInfoRow2').getChildByName('UserGoldCoinValue').getComponent(Label);
    this.winNumText = this.node.getChildByName('UserInfoRow3').getChildByName('UserWinNumValue').getComponent(Label);
    this.loseNumText = this.node.getChildByName('UserInfoRow4').getChildByName('UserLoseNumValue').getComponent(Label);
    this.winRateText = this.node.getChildByName('UserInfoRow5').getChildByName('UserWinRateValue').getComponent(Label);
    
    this.userInfoImage = this.node.getChildByName('UserHeaderInfo').getChildByName('UserHeaderImage');

    this.userLevelLogoSprite = this.node.getChildByName('UserLevel').getChildByName('LevelLogoSprite');
    this.userLevelLogoText = this.node.getChildByName('UserLevel').getChildByName('LevelNameLabel');

    this.hasLoadLevelImage = false;

    new UserInfo().createUserInfo();

    this.userLevelLogoSprite.on(Node.EventType.TOUCH_END, this.showLevelIntroduction, this);
    this.userLevelLogoText.on(Node.EventType.TOUCH_END, this.showLevelIntroduction, this);
  }
  onEnable () {
    WxSdkManager.toggleCustomCol33Ad(true)
    input.on(Input.EventType.TOUCH_END, this.hideLevelIntroduction, this);
  }




  protected onDisable(): void {
  //  console.log("777777777777777777")
    WxSdkManager.toggleCustomCol33Ad(false)
  }
  


  update(deltaTime: number) {
    this.updateUserHeader();

    this.playLevelText.string = `${databus.selfInfo.playLevel.playLevel} 级`;
    this.goldCoinText.string = databus.selfInfo.getGoldCoinDesc();
    this.winNumText.string = `${databus.selfInfo.winNum} 局`;
    this.loseNumText.string = `${databus.selfInfo.loseNum} 局`;
    var rate = 0.00;
    if (databus.selfInfo.winNum + databus.selfInfo.loseNum > 0) {
      rate = Math.round(100 * databus.selfInfo.winNum / (databus.selfInfo.winNum + databus.selfInfo.loseNum));
    }
    this.winRateText.string = `${rate} %`;
  }

  updateUserHeader() {
    const headSpriteFrame = ResourceManager.Instance().getImageSpriteFrame(ResourceManager.SelfUserHeaderImage);
    const userHeaderSprite = this.userInfoImage.getComponent(Sprite);
    userHeaderSprite.spriteFrame = headSpriteFrame;

    // 更新昵称和账号
    const nickName = this.node.getChildByName('UserHeaderInfo').getChildByName('UserNickName').getComponent(Label);
    // 截取前4个字
    nickName.string = databus.selfInfo.showNickName();

    // 更新账号信息
    const userId = this.node.getChildByName('UserHeaderInfo').getChildByName('UserID').getComponent(Label);
    userId.string = databus.selfInfo.showUserId();
    

    // 更新等级信息
    const userLevelName = this.node.getChildByName('UserLevel').getChildByName('LevelNameLabel').getComponent(Label);
    userLevelName.string = databus.selfInfo.playLevel.playLevelName();

    const levelLogoName = databus.selfInfo.playLevel.levelLogoName();
    const logoSpriteFrame = ResourceManager.Instance().getImageSpriteFrame(levelLogoName);
    if (!this.hasLoadLevelImage && logoSpriteFrame) {
      const userLogoSprite = this.node.getChildByName('UserLevel').getChildByName('LevelLogoSprite').getComponent(Sprite);
      userLogoSprite.spriteFrame = logoSpriteFrame;
      this.hasLoadLevelImage = true;
    }
  }

  public backPage() {
    databus.sceneStack.backScene();
  }

  public showLevelIntroduction() {
    this.node.getChildByName('LevelIntroSprite').active = true;
    this.node.getChildByName('LevelIntroSprite').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(ResourceManager.GameLevelImage);
  }
  public hideLevelIntroduction() {
    this.node.getChildByName('LevelIntroSprite').active = false;
  }
}